
public class Reader extends Thread {

    private final Database database;
    private final int readerNumber;

    public Reader(Database database, int readerNumber) {
        this.database = database;
        this.readerNumber = readerNumber;
    }

    @Override
    public void run() {
        while (true) {
            
            try {
                database.startRead();
            } catch (InterruptedException ex) {
            }

            int readTime = (int) (Math.random() * 10);
            System.out.println("Reader " + readerNumber + " reads on database for " + readTime + " seconds");

            try {
                Thread.sleep(readTime * 1000);
            } catch (InterruptedException e) {
            }

            try {
                database.endRead();
            } catch (InterruptedException ex) {
            }
            System.out.println("Reader " + readerNumber + " leaves database");
        }
    }
}
